import 'package:amap_flutter_base/amap_flutter_base.dart';
import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:flutter/material.dart';

class ChoiceEnhance extends StatefulWidget {
  // ChoiceEnhance({Key key}) : super(key: key);

  @override
  _ChoiceEnhanceState createState() => _ChoiceEnhanceState();
}

class _ChoiceEnhanceState extends State<ChoiceEnhance> {
  CameraPosition _kInitialPosition = const CameraPosition(
    target: LatLng(39.909187, 116.397451),
    zoom: 16.0,
  );

  final Map<String, Marker> _initMarkerMap = <String, Marker>{};

  Object _mapController = Object();
  void onMapCreated(AMapController controller) {
    setState(() {
      _mapController = controller;
    });
  }

  @override
  Widget build(BuildContext context) {
    Marker marker = Marker(
        position: LatLng(39.909187, 116.397451),
        draggable: true,
        onDragEnd: (id, latLng) {
          print('拖拽结果');
          print(id);
          print(latLng);
        });
    _initMarkerMap[marker.id] = marker;

    final AMapWidget map = AMapWidget(
      initialCameraPosition: _kInitialPosition,
      onMapCreated: onMapCreated,
      myLocationStyleOptions: MyLocationStyleOptions(
        true,
      ),
      markers: Set<Marker>.of(_initMarkerMap.values),
      onCameraMove: (argument) {
        print('onCameraMove...');
        print(argument);
      },
    );

    return Scaffold(
      appBar: AppBar(
        title: Text('地图选点（增强）'),
        actions: [
          GestureDetector(
            child: Container(
              margin: EdgeInsets.only(right: 12),
              child: Icon(Icons.share),
            ),
            onTap: () {},
          )
        ],
      ),
      body: Stack(
        children: [
          Container(
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            child: map,
          ),
          Row(
            children: [
              Container(
                margin: EdgeInsets.only(left: 12, top: 12),
                padding: EdgeInsets.all(4),
                child: Text(
                  '长按拖动起点',
                ),
                decoration: BoxDecoration(
                  color: Colors.white70,
                  borderRadius: const BorderRadius.all(Radius.circular(12)),
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 12, top: 12),
                padding: EdgeInsets.all(4),
                child: Text(
                  '长按拖动终点',
                ),
                decoration: BoxDecoration(
                  color: Colors.white70,
                  borderRadius: const BorderRadius.all(Radius.circular(12)),
                ),
              ),
            ],
          ),
        ],
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.startFloat,
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.play_arrow),
        onPressed: () {},
      ),
    );
  }
}
